﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RssBar
{
    /// <summary>
    /// Interaction logic for OptionsWindow.xaml
    /// </summary>
    public partial class OptionsWindow : Window
    {
        public OptionStatus Status { get; private set; }

        private List<IOptionsContentPage> aviableContentPages = new List<IOptionsContentPage>();
        private int currentPageSelected = 0;
        private bool closeConfirmed = false;

        public OptionsWindow(OptionsContentService ocsService)
        {
            InitializeComponent();
            Status = OptionStatus.Processing;

            if (ocsService != null)
                aviableContentPages.AddRange(ocsService.RetrieveOptionContent());

            foreach (IOptionsContentPage p in aviableContentPages)
            {
                Label l = new Label();
                l.Content = p.TitleText;
                l.Margin = new Thickness(0, 2, 0, 0);
                l.Height = 25;
                l.Tag = aviableContentPages.IndexOf(p);
                l.MouseUp += new MouseButtonEventHandler(l_MouseUp);
                spNavigationButtons.Children.Add(l);
            }

            spPage.Children.Add(aviableContentPages[currentPageSelected] as UserControl);
        }

        void l_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Label l = sender as Label;
            if (l != null)
            {
                spPage.Children.Clear();
                spPage.Children.Add(GetPageByIndex(Int32.Parse(l.Tag.ToString())) as UserControl);
                SetLabelColors();
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Status = OptionStatus.Cancelled;
            closeConfirmed = false;
            this.Close();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            bool resultSettings = true;
            foreach (IOptionsContentPage ocp in aviableContentPages)
            {
                if (!ocp.Apply())
                {
                    resultSettings = false;
                    break;
                }
            }

            if (resultSettings)
            {
                Configuration.Save();

                closeConfirmed = true;
                Status = OptionStatus.Finished;
                this.Close();
            }
        }

        private IOptionsContentPage GetPageByIndex(int index)
        {
            if (index >= 0 && index < aviableContentPages.Count)
            {
                currentPageSelected = index;
                return aviableContentPages[index];
            }
            return null;
        }

        private void SetLabelColors()
        {
            SolidColorBrush blueBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("Blue"));
            SolidColorBrush blackBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("Black"));

            foreach (object o in spNavigationButtons.Children)
            {
                Label l = o as Label;
                if (l != null && Int32.Parse(l.Tag.ToString()) == currentPageSelected)
                    l.Foreground = blueBrush;
                else
                    l.Foreground = blackBrush;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!closeConfirmed)
            {
                Status = OptionStatus.Cancelled;
            }
        }
    }

    public enum OptionStatus
    {
        Processing,
        Cancelled,
        Finished
    }

    public class OptionsContentService
    {
        private Dictionary<int, IOptionsContentPage> content = new Dictionary<int, IOptionsContentPage>();

        public OptionsContentService()
        {
            content.Add(0, new ucOptionsApplication());
            content.Add(1, new ucOptionsChannel());
            content.Add(2, new ucOptionsProxy());
        }

        public List<IOptionsContentPage> RetrieveOptionContent()
        {
            return (from c in content orderby c.Key select c.Value).ToList<IOptionsContentPage>();
        }
    }

    public interface IOptionsContentPage
    {
        string TitleText { get; }

        bool Apply();
    }
}
